package com.sc.nft.controller;




import cn.hutool.core.util.StrUtil;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.controller.request.IdRequest;
import com.sc.nft.entity.SaasUserTask;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.dto.SaasTaskWebPageDTO;
import com.sc.nft.entity.dto.SaasUserTaskParticipatingTaskDTO;
import com.sc.nft.entity.dto.SaasUserWaitingToBeClaimedTaskDTO;
import com.sc.nft.entity.vo.*;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.SaasTaskService;
import com.sc.nft.sup.PageRequest;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import com.tencentcloudapi.trtc.v20190722.models.McuUserInfoParams;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 任务基础表(SaasTask)表控制层
 *
 * @author 王伟成
 * @since 2024-01-16 15:55:21
 */

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/saasTask")
@Api(tags = "用户任务")
public class SaasTaskController {
    /**
     * 服务对象
     */
    private final SaasTaskService saasTaskService;

    private final Redisson redisson;



    @ApiOperation(value = "用户saas任务前端分页获取数据", notes = "用户saas任务前端分页获取数据", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/saasTaskWebPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<SaasTaskWebPageVO>saasTaskWebPage(@RequestBody SaasTaskWebPageDTO saasTaskWebPageDTO){
        return PageResult.success(saasTaskService.saasTaskWebPage(saasTaskWebPageDTO, UserPool.getUser()));
    }

    @ApiOperation(value = "任务详情(列表)", notes = "任务详情(未完成)", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/saasTaskById", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<SaasTaskWebDetailsVO> saasTaskById(@Valid @RequestBody IdRequest idRequest){
        return Result.success(saasTaskService.saasTaskById(idRequest.getId(), UserPool.getUser()));
    }

    @ApiOperation(value = "任务详情(我参与的)", notes = "任务详情(我参与的)", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/saasTaskByIdCompleted", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<SaasTaskWebDetailsVO> saasTaskByIdCompleted(@Valid @RequestBody IdRequest idRequest){
        return Result.success(saasTaskService.saasTaskByIdCompleted(idRequest.getId(), UserPool.getUser()));
    }
    @ApiOperation(value = "用户参与的任务", notes = "用户参与的任务", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/userParticipatingTask", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<SaasUserTaskParticipatingTaskVO> userParticipatingTask(@Valid @RequestBody SaasUserTaskParticipatingTaskDTO saasUserTaskParticipatingTaskDTO){
        return PageResult.success(saasTaskService.userParticipatingTask(saasUserTaskParticipatingTaskDTO,UserPool.getUser()));
    }


    @ApiOperation(value = "用户待领取任务和已领取", notes = "用户待领取任务z和已领取", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/userWaitingToBeClaimedTask", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<SaasUserWaitingToBeClaimedTaskVO> userWaitingToBeClaimedTask(@Valid @RequestBody SaasUserWaitingToBeClaimedTaskDTO saasUserTaskParticipatingTaskDTO){
        return PageResult.success(saasTaskService.userWaitingToBeClaimedTask(saasUserTaskParticipatingTaskDTO,UserPool.getUser()));
    }


    @ApiOperation(value = "根据用户任务完成获取奖励", notes = "根据用户任务完成获取奖励", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getByUserTaskIdGainAward", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<List<SaasUserTaskIdGainAwardVO>> getByUserTaskIdGainAward(@Valid @RequestBody IdRequest idRequest){
        return Result.success(saasTaskService.getByUserTaskIdGainAward(idRequest.getId()));
    }

    @SneakyThrows
    @ApiOperation(value = "领取奖励", notes = "领取奖励", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/receiveAward", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result redemptionReward(@Valid @RequestBody IdRequest idRequest){
        String key = RedisKeyEnum.REDEMPTION_REWARD.getKey();
        String lockKey = StrUtil.format("{}:{}:{}", key, idRequest.getId(), UserPool.getUser().getId());
        UserInfo user = UserPool.getUser();
        RLock lock = redisson.getLock(lockKey);
        boolean b = lock.tryLock(10, TimeUnit.SECONDS);
        if (!b) {
            throw new GlobalRunTimeException("请勿重复提交");
        }

        try {
            saasTaskService.receiveAward(idRequest.getId(),user);
        }catch (Exception e){
            log.error("领取奖励异常",e);
            throw new GlobalRunTimeException(e.getMessage());
        }finally {
            lock.unlock();
        }
        return Result.success();
    }

    @SneakyThrows
    @ApiOperation(value = "任务模板超长", notes = "任务模板超长", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/taskTemplateIsTooLongProcedure", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<List<SaasTaskBuyVO>> taskTemplateIsTooLongProcedure(@Valid @RequestBody IdRequest idRequest){
        return Result.success(saasTaskService.taskTemplateIsTooLongProcedure(idRequest.getId()));
    }


    @ApiOperation(value = "用户saas星球任务前端分页获取数据", notes = "用户saas星球任务前端分页获取数据", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/saasTaskStarWebPage", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<SaasTaskWebPageVO>saasTaskStarWebPage(@RequestBody PageRequest pageRequest){
        return PageResult.success(saasTaskService.saasTaskStarWebPage(pageRequest));
    }
}
